home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources-CPlus / cp-spew.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-18  |  27.3 KB  |  1,065 lines  |  [TEXT/MPS ]

  1. /* Type Analyzer for GNU C++.
  2.    Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
  3.    Hacked... nay, bludgeoned... by Mark Eichin (eichin@cygnus.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. /* This file is the type analyzer for GNU C++.  To debug it, define SPEW_DEBUG
  23.    when compiling cp-parse.c and cp-spew.c.  */
  24.  
  25. #include "config.h"
  26. #include <stdio.h>
  27. #include "input.h"
  28. #include "tree.h"
  29. #include "cp-lex.h"
  30. #include "cp-parse.h"
  31. #include "cp-tree.h"
  32. #include "flags.h"
  33. #include "obstack.h"
  34.  
  35. /* This takes a token stream that hasn't decided much about types and
  36.    tries to figure out as much as it can, with excessive lookahead and
  37.    backtracking. */
  38.  
  39. /* fifo of tokens recognized and available to parser. */
  40. struct token  {
  41.   /* The values for YYCHAR will fit in a short.  */
  42.   short        yychar;
  43.   short        end_of_file;
  44.   YYSTYPE    yylval;
  45. };
  46.  
  47. static int do_aggr ();
  48. static struct token frob_identifier ();
  49. static struct token hack_scope ();
  50. static tree hack_ptype ();
  51. static tree hack_more_ids ();
  52.  
  53. /* From cp-lex.c: */
  54. /* the declaration found for the last IDENTIFIER token read in.
  55.    yylex must look this up to detect typedefs, which get token type TYPENAME,
  56.    so it is left around in case the identifier is not a typedef but is
  57.    used in a context which makes it a reference to a variable.  */
  58. extern tree lastiddecl;        /* let our brains leak out here too */
  59. extern int    yychar;        /*  the lookahead symbol        */
  60. extern YYSTYPE    yylval;        /*  the semantic value of the        */
  61.                 /*  lookahead symbol            */
  62. extern int end_of_file;
  63.  
  64. struct obstack token_obstack;
  65. int first_token;
  66.   
  67. #ifdef SPEW_DEBUG
  68. int spew_debug = 0;
  69. static unsigned int yylex_ctr = 0;
  70. static int debug_yychar ();
  71. #endif
  72.  
  73. static char follows_typename[END_OF_SAVED_INPUT+1];
  74. static char follows_identifier[END_OF_SAVED_INPUT+1];
  75.  
  76. /* This is a hack!!! TEMPLATE_TYPE_SEEN_BEFORE_SCOPE consists of the name
  77.  * of the last template_type parsed in cp-parse.y if it is followed by a
  78.  * scope operator.  It will be reset inside the next invocation of yylex().
  79.  * This is used for recognizing nested types inside templates.
  80.  * - niklas@appli.se */
  81. tree template_type_seen_before_scope;
  82.  
  83. /* Initialize token_obstack. Called once, from init_lex.  */
  84. void
  85. init_spew ()
  86. {
  87.   static char *chars_following_identifier = ".+-|/%^!?:";
  88.   short *ps;
  89.   static short toks_follow_ids[] =
  90.     { POINTSAT_LEFT_RIGHT, ASSIGN, RANGE, OROR, ANDAND, MIN_MAX, EQCOMPARE,
  91.       ARITHCOMPARE, LSHIFT, RSHIFT, UNARY, PLUSPLUS, MINUSMINUS, POINTSAT,
  92.       POINTSAT_STAR, DOT_STAR, CONSTANT, STRING, SIZEOF, ENUM, IF,
  93.       ELSE, WHILE, DO, FOR, SWITCH, CASE, DEFAULT, BREAK, CONTINUE,
  94.       RETURN, GOTO, ASM_KEYWORD, TYPEOF, ALIGNOF, HEADOF, CLASSOF, ATTRIBUTE,
  95.       AGGR, VISSPEC, DELETE, RAISE, RERAISE, TRY, EXCEPT, CATCH,
  96.       THROW, ANSI_TRY, ANSI_THROW, EXTERN_LANG_STRING, ALL,
  97.       END_OF_SAVED_INPUT, -1 };
  98.   static short toks_follow_types[] =
  99.     { IDENTIFIER, TYPENAME, SCOPED_TYPENAME, SCSPEC, TYPESPEC, TYPE_QUAL,
  100.       ELLIPSIS, THIS, OPERATOR, DYNAMIC, TEMPLATE, SCOPE, START_DECLARATOR,
  101.       TYPENAME_COLON, PAREN_STAR_PAREN, TYPENAME_ELLIPSIS, PTYPENAME,
  102.       PRE_PARSED_FUNCTION_DECL, PRE_PARSED_CLASS_DECL, -1 };
  103.  
  104.   gcc_obstack_init(&token_obstack);
  105.  
  106.   /* Initialize the arrays saying what tokens are definitely
  107.      (or possibly) valid following typenames and identifiers.  */
  108.   while (*chars_following_identifier)
  109.     follows_identifier[*chars_following_identifier++] = 1;
  110.   for (ps = toks_follow_ids; *ps != -1; ps++)
  111.     follows_identifier[*ps] = 1;
  112.   for (ps = toks_follow_types; *ps != -1; ps++)
  113.     follows_typename[*ps] = 1;
  114. }
  115.  
  116. #ifdef SPEW_DEBUG
  117. /* Use functions for debugging...  */
  118.  
  119. /* Return the number of tokens available on the fifo. */
  120. static int
  121. num_tokens ()
  122. {
  123.   return (obstack_object_size(&token_obstack)/sizeof(struct token))
  124.     - first_token;
  125. }
  126.  
  127. /* Fetch the token N down the line from the head of the fifo. */
  128. static struct token*
  129. nth_token (n)
  130.      int n;
  131. {
  132.   /* could just have this do slurp_ implicitly, but this way is easier
  133.    * to debug... */
  134.   my_friendly_assert (n < num_tokens(), 298);
  135.   return ((struct token*)obstack_base(&token_obstack))+n+first_token;
  136. }
  137.  
  138. /* Add a token to the token fifo. */
  139. static void
  140. add_token (t)
  141.      struct token* t;
  142. {
  143.   obstack_grow(&token_obstack,t,sizeof (struct token));
  144. }
  145.  
  146. /* Consume the next token out of the fifo.  */
  147. static void
  148. consume_token()
  149. {
  150.   if (num_tokens() == 1)
  151.     {
  152.       obstack_free(&token_obstack, obstack_base (&token_obstack));
  153.       first_token = 0;
  154.     }
  155.   else
  156.     first_token++;
  157. }
  158.  
  159. #else
  160. /* ...otherwise use macros.  */
  161.  
  162. #define num_tokens() \
  163.   ((obstack_object_size(&token_obstack)/sizeof(struct token)) - first_token)
  164.  
  165. #define nth_token(N) \
  166.   (((struct token*)obstack_base(&token_obstack))+(N)+first_token)
  167.  
  168. #define add_token(T) obstack_grow(&token_obstack, (T), sizeof (struct token))
  169.  
  170. #define consume_token() \
  171.   (num_tokens() == 1                            \
  172.    ? (obstack_free (&token_obstack, obstack_base (&token_obstack)),    \
  173.       (first_token = 0))                        \
  174.    : first_token++)
  175. #endif
  176.  
  177. /* Pull in enough tokens from real_yylex that the queue is N long.  */
  178.  
  179. static void
  180. scan_tokens (n)
  181.      int n;
  182. {
  183.   int i;
  184.   struct token *tmp;
  185.  
  186.   /* We cannot read past certain tokens, so make sure we don't.  */
  187.   i = num_tokens ();
  188.   if (i > n)
  189.     return;
  190.   while (i-- > 0)
  191.     {
  192.       tmp = nth_token (i);
  193.       /* Never read past these characters: they might separate
  194.      the current input stream from one we save away later.  */
  195.       if (tmp->yychar == '{' || tmp->yychar == ':')
  196.     goto pad_tokens;
  197.     }
  198.  
  199.   while (num_tokens() <= n)
  200.     {
  201.       obstack_blank(&token_obstack,sizeof (struct token));
  202.       tmp = ((struct token *)obstack_next_free (&token_obstack))-1;
  203.       tmp->yychar = real_yylex();
  204.       tmp->end_of_file = end_of_file;
  205.       tmp->yylval = yylval;
  206.       end_of_file = 0;
  207.       if (tmp->yychar == '{'
  208.       || tmp->yychar == ':'
  209.       || tmp->yychar == ';')
  210.     {
  211.     pad_tokens:
  212.       while (num_tokens () <= n)
  213.         {
  214.           obstack_blank(&token_obstack,sizeof (struct token));
  215.           tmp = ((struct token *)obstack_next_free (&token_obstack))-1;
  216.           tmp->yychar = EMPTY;
  217.           tmp->end_of_file = 0;
  218.         }
  219.     }
  220.     }
  221. }
  222.  
  223. /* Create room for N tokens at the front of the fifo.  This is used
  224.    to insert new tokens into the stream ahead of the current token.  */
  225.  
  226. static void
  227. shift_tokens (n)
  228.      int n;
  229. {
  230.   if (first_token >= n)
  231.     first_token -= n;
  232.   else
  233.     {
  234.       int old_token_count = num_tokens ();
  235.       char *tmp;
  236.  
  237.       obstack_blank (&token_obstack, (n-first_token) * sizeof (struct token));
  238.       if (old_token_count)
  239.     {
  240.       tmp = (char *)alloca ((num_tokens () + (n-first_token))
  241.                 * sizeof (struct token));
  242.       /* This move does not rely on the system being able to handle
  243.          overlapping moves.  */
  244.       bcopy (nth_token (0), tmp, old_token_count * sizeof (struct token));
  245.       bcopy (tmp, nth_token (n), old_token_count * sizeof (struct token));
  246.     }
  247.       first_token = 0;
  248.     }
  249. }
  250.  
  251. int
  252. probe_obstack (h, obj, nlevels)
  253.      struct obstack *h;
  254.      tree obj;
  255.      unsigned int nlevels;
  256. {
  257.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  258.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  259.  
  260.   lp = (h)->chunk;
  261.   /* We use >= rather than > since the object cannot be exactly at
  262.      the beginning of the chunk but might be an empty object exactly
  263.      at the end of an adjacent chunk. */
  264.   for (; nlevels > 0 && lp != 0 && ((tree)lp >= obj || (tree)lp->limit < obj);
  265.        nlevels -= 1)
  266.     {
  267.       plp = lp->prev;
  268.       lp = plp;      
  269.     }
  270.   return nlevels > 0 && lp != 0;
  271. }
  272.  
  273. /* from cp-lex.c: */
  274. /* Value is 1 if we should try to make the next identifier look like a
  275.    typename (when it may be a local variable or a class variable).
  276.    Value is 0 if we treat this name in a default fashion.
  277.    Value is -1 if we must not see a type name.  */
  278. extern int looking_for_typename;
  279.  
  280. extern struct obstack *current_obstack, *saveable_obstack;
  281.  
  282. #ifdef MPW
  283. #pragma segment CPSPEW01
  284. #endif
  285.  
  286. int
  287. yylex()
  288. {
  289.   struct token tmp_token;
  290.   tree trrr;
  291.  
  292.  retry:
  293. #ifdef SPEW_DEBUG
  294.   if (spew_debug)
  295.   {
  296.     yylex_ctr ++;
  297.     fprintf(stderr, "\t\t## %d ##",yylex_ctr);
  298.   }
  299. #endif
  300.   
  301.   /* This is a kludge for recognizing nested types in templates */
  302.   if (template_type_seen_before_scope)
  303.     {
  304.       shift_tokens (2);        /* Sync in hack_more_ids (yes, it's ugly) */
  305.       nth_token (1)->yychar = SCOPE;
  306.       yylval.ttype = hack_more_ids (0, template_type_seen_before_scope);
  307.       template_type_seen_before_scope = 0;
  308.       if (!yylval.ttype)
  309.     {
  310.       /* Sync back again, leaving SCOPE on the token stream, because we
  311.        * failed to substitute the original SCOPE token with a
  312.        * SCOPED_TYPENAME.  See rule "template_type" in cp-parse.y */
  313.       consume_token ();
  314.     }
  315.       else
  316.     {
  317.       yychar = SCOPED_TYPENAME;
  318. #ifdef SPEW_DEBUG    
  319.       if (spew_debug)
  320.         debug_yychar(yychar);
  321. #endif
  322.       return yychar;
  323.     }
  324.     }
  325.  
  326.   /* if we've got tokens, send them */
  327.   if (num_tokens())
  328.     {
  329.       tmp_token= *nth_token(0);
  330.  
  331.       /* TMP_TOKEN.YYLVAL.TTYPE may have been allocated on the wrong obstack.
  332.      If we don't find it in CURRENT_OBSTACK's current or immediately
  333.      previous chunk, assume it was and copy it to the current obstack.  */
  334.       if ((tmp_token.yychar == CONSTANT
  335.        || tmp_token.yychar == STRING)
  336.       && ! TREE_PERMANENT (tmp_token.yylval.ttype)
  337.       && ! probe_obstack (current_obstack, tmp_token.yylval.ttype, 2)
  338.       && ! probe_obstack (saveable_obstack, tmp_token.yylval.ttype, 2))
  339.     tmp_token.yylval.ttype = copy_node (tmp_token.yylval.ttype);
  340.     }
  341.   else
  342.     {
  343.       /* if not, grab the next one and think about it */
  344.       tmp_token.yychar = real_yylex ();
  345.       tmp_token.yylval = yylval;
  346.       tmp_token.end_of_file = end_of_file;
  347.       add_token(&tmp_token);
  348.     }
  349.  
  350.   /* many tokens just need to be returned. At first glance, all we
  351.    * have to do is send them back up, but some of them are needed to
  352.    * figure out local context. */
  353.   switch(tmp_token.yychar)
  354.     {
  355.     case EMPTY:
  356.       /* This is a lexical no-op.  */
  357.       consume_token ();
  358. #ifdef SPEW_DEBUG    
  359.       if (spew_debug)
  360.     debug_yychar (tmp_token.yychar);
  361. #endif
  362.       goto retry;
  363.  
  364.     case IDENTIFIER:
  365.       /* Note: this calls arbitrate_lookup.  */
  366.       trrr = lookup_name (tmp_token.yylval.ttype, -1);
  367.       if (trrr)
  368.     {
  369.       tmp_token.yychar = identifier_type (trrr);
  370.       switch (tmp_token.yychar)
  371.         {
  372.         case TYPENAME:
  373.           lastiddecl = IDENTIFIER_TYPEDECL_VALUE (tmp_token.yylval.ttype);
  374.           break;
  375.         case IDENTIFIER:
  376.           lastiddecl = trrr;
  377.           break;
  378.         case PTYPENAME:
  379.           /* This is for cases like
  380.             template<class A> X<A>::operator[] ...
  381.          since "X" is (presumably) a PTYPENAME; we might want to
  382.          avoid seeing the entire thing as a type name, but X<A>
  383.          must be one.
  384.  
  385.          It might not work right if the thing after the ::
  386.          can be a typename nested in X<A>, but I don't think the
  387.          PT code would be up to dealing with that anyways.  --KR  */
  388.           if (looking_for_typename == -1)
  389.         {
  390.           scan_tokens (2);
  391.           if (nth_token(1)->yychar == '<')
  392.             looking_for_typename = 0;
  393.         }
  394.           break;
  395.         default:
  396.           my_friendly_abort (101);
  397.         }
  398.     }
  399.       else
  400.     lastiddecl = trrr;
  401.       /* and fall through to... */
  402.     case TYPENAME:
  403.     case PTYPENAME:
  404.       /* if (new_token) add_token (&tmp_token); */
  405.       *nth_token(0) = tmp_token;
  406.       tmp_token = frob_identifier ();
  407.       if (looking_for_typename < 0)
  408.     {
  409.       tmp_token.yychar = IDENTIFIER;
  410.       lastiddecl = 0;
  411.       looking_for_typename = 0;
  412.     }
  413.       else if (lastiddecl && TREE_CODE (lastiddecl) == TYPE_DECL)
  414.     {
  415.       scan_tokens (2);
  416.       if (nth_token(0)->yychar == IDENTIFIER
  417.           && nth_token (1)->yychar != SCOPE)
  418.         looking_for_typename = -1;
  419.       else
  420.         looking_for_typename = 0;
  421.       goto finish_typename_processing;
  422.     }
  423.       else
  424.     looking_for_typename = 0;
  425.       break;
  426.  
  427.     case TYPESPEC:
  428.     case SCSPEC:
  429.       consume_token ();
  430.     finish_typename_processing:
  431.       /* Now see if we should insert a START_DECLARATOR token.
  432.          Here are the cases caught:
  433.  
  434.      typespec ( * ID ) (    // ptr to function
  435.      typespec ( & ID ) (    // ref to function
  436.      typespec ( * ID ) [    // array of pointers
  437.      typespec ( & ID ) [    // array of references
  438.  
  439.      This is a terrible kludge.  */
  440.  
  441.       scan_tokens (2);
  442.       if (nth_token (0)->yychar == '('
  443.       && (nth_token (1)->yychar == '*'
  444.           || nth_token (1)->yychar == '&'))
  445.     {
  446.       scan_tokens (5);
  447.       if (nth_token (3)->yychar == ')'
  448.           && (nth_token (4)->yychar == '('
  449.           || nth_token (4)->yychar == '['
  450.           || nth_token (4)->yychar == LEFT_RIGHT)
  451.           && (nth_token (2)->yychar == IDENTIFIER
  452.           || nth_token (2)->yychar == TYPENAME))
  453.         {
  454.           shift_tokens (1);
  455.           nth_token (0)->yychar = START_DECLARATOR;
  456.         }
  457.     }
  458.       break;
  459.  
  460. #if 0
  461.     case '(':
  462.       /* Handle casts.  We are looking for one of:
  463.          `( TYPENAME' followed by `)', or
  464.      `( TYPENAME *' followed by one of `[,*,&,)', or
  465.      `( TYPENAME &' followed by one of `[,*,&,)', or
  466.      `( TYPENAME [' followed by `]'.  We are punting
  467.      generality on scanning casts to array types.  */
  468.       scan_tokens (4);
  469.       if (nth_token (1)->yychar == IDENTIFIER)
  470.     {
  471.       tree type = identifier_typedecl_value (nth_token (1)->yylval.ttype);
  472.       if (type)
  473.         switch (nth_token (2)->yychar)
  474.           {
  475.           default:
  476.         break;
  477.           }
  478.     }
  479.       break;
  480.  
  481.     case SCOPE:
  482.       /* if (new_token) add_token (&tmp_token); */
  483.       *nth_token(0) = tmp_token;
  484.       tmp_token = hack_scope ();
  485.       break;
  486. #endif
  487.  
  488.     case AGGR:
  489.       *nth_token(0) = tmp_token;
  490.       do_aggr ();
  491.       /* fall through to output... */
  492.     case ENUM:
  493.       /* Set this again, in case we are rescanning.  */
  494.       looking_for_typename = 1;
  495.       /* fall through... */
  496.     default:
  497. #ifdef SPEW_DEBUG    
  498.       if (spew_debug)
  499.     debug_yychar(tmp_token.yychar);
  500. #endif
  501.       consume_token();
  502.       yylval = tmp_token.yylval;
  503.       yychar = tmp_token.yychar;
  504.       end_of_file = tmp_token.end_of_file;
  505.       return tmp_token.yychar;
  506.     }
  507.  
  508.   if (tmp_token.yychar == SCOPED_TYPENAME)
  509.     {
  510. #if 0
  511.       tree t2 = resolve_scope_to_name (NULL_TREE, tmp_token.yylval.ttype);
  512.       if (t2 != NULL_TREE)
  513.     {
  514.       tmp_token.yylval.ttype = t2;
  515.       tmp_token.yychar = TYPENAME;
  516.     }
  517.       else
  518.     {
  519.       /* unwind? */
  520.     }
  521.     }
  522.   else
  523.     {
  524.       /* couldn't get here, as is... */
  525. #endif
  526.       tmp_token.yychar = TYPENAME;
  527.     }
  528.  
  529.   yylval = tmp_token.yylval;
  530.   yychar = tmp_token.yychar;
  531.   end_of_file = tmp_token.end_of_file;
  532. #ifdef SPEW_DEBUG    
  533.   if (spew_debug)
  534.     debug_yychar(yychar);
  535. #endif
  536. /*  consume_token(); */ /* already eaten by frob_identifier?... */
  537.   return yychar;
  538. }
  539.  
  540. /* token[0] == AGGR (struct/union/enum)
  541.  * thus, token[1] is either a TYPENAME or a TYPENAME_DEFN
  542.  * if token[2] == '{' or ':' then it's TYPENAME_DEFN
  543.  */
  544. static int
  545. do_aggr ()
  546. {
  547.   int yc1, yc2;
  548.   
  549.   scan_tokens (2);
  550.   yc1 = nth_token (1)->yychar;
  551.   if (yc1 != TYPENAME && yc1 != IDENTIFIER && yc1 != PTYPENAME)
  552.     return 0;
  553.   yc2 = nth_token (2)->yychar;
  554.   if (yc2 == '{' || yc2 == ':')
  555.     {
  556.       switch (yc1)
  557.     {
  558.     case TYPENAME:
  559.       nth_token (1)->yychar = TYPENAME_DEFN;
  560.       break;
  561.     case PTYPENAME:
  562.       nth_token (1)->yychar = PTYPENAME_DEFN;
  563.       break;
  564.     case IDENTIFIER:
  565.       nth_token (1)->yychar = IDENTIFIER_DEFN;
  566.       break;
  567.     default:
  568.       my_friendly_abort (102);
  569.     }
  570.     }
  571.   return 0;
  572. }  
  573.  
  574. static struct token
  575. frob_identifier ()
  576. {
  577.   /* we could have a type, if it is followed by :: (if so, suck it all up); */
  578.   /* we could have a ptypename; */
  579.   /* we could have a normal identifier. */
  580.   tree t1;
  581.   struct token rt;
  582.   
  583.   scan_tokens(1);
  584.   rt = *nth_token(0);
  585.  
  586. #if 0
  587.   if (nth_token(1)->yychar == '<')
  588.     {
  589.       t1 = hack_ptype();    /* suck up the whole thing */
  590.       if (t1)
  591.     {
  592.       rt.yylval.ttype = t1;
  593.       rt.yychar = TYPENAME;
  594.       *nth_token(0) = rt;
  595.     }
  596.       /* else fall out bottom */
  597.     }    
  598. #endif
  599.  
  600.   if (nth_token(1)->yychar == SCOPE)
  601.     {
  602. #if 0
  603.       t1 = hack_more_ids(0);
  604.       if (t1 && TREE_CODE(t1) == SCOPE_REF)
  605. #else
  606.       t1 = hack_more_ids(0, nth_token (0)->yylval.ttype);
  607.       if (t1)
  608. #endif
  609.     {
  610.       rt.yylval.ttype = t1;
  611.       rt.yychar = SCOPED_TYPENAME ;
  612.       return rt;
  613.     }
  614.       else
  615.     {
  616.       /* deal with types (enums?) in classes... */
  617.       struct token *tok;
  618.       tree ta, tb;
  619.       scan_tokens(3);
  620.  
  621.       /* Have to check for a type conversion operator
  622.          to a nested type.  */
  623.       if (nth_token (2)->yychar == OPERATOR)
  624.         tok = nth_token (3);
  625.       else
  626.         tok = nth_token(2);
  627.  
  628.       if (tok->yychar == IDENTIFIER || tok->yychar == TYPENAME)
  629.         {
  630.           ta = build_parse_node (SCOPE_REF,
  631.                      nth_token(0)->yylval.ttype,
  632.                      tok->yylval.ttype);
  633.           tb = resolve_scope_to_name (NULL_TREE, ta);
  634.  
  635.           if (tb != NULL_TREE)
  636.         {
  637.           if (nth_token (2)->yychar == OPERATOR)
  638.             {
  639.               /* Have to keep these tokens around
  640.              so we can finish parsing the declaration.
  641.              What do we do for
  642.  
  643.              int foo::operator bar::baz (); 
  644.  
  645.              where bar is a nested class in foo?  */
  646.               nth_token (3)->yychar = TYPENAME;
  647.               nth_token (3)->yylval.ttype = tb;
  648.             }
  649.           else
  650.             {
  651.               consume_token (); /* base type */
  652.               consume_token (); /* SCOPE */
  653.               consume_token (); /* member type */
  654.               rt.yychar = TYPENAME;
  655.               rt.yylval.ttype = tb;
  656.               rt.end_of_file = tok->end_of_file;
  657.               return rt;
  658.             }
  659.           
  660.         }
  661.         }      
  662.       /* else fall out bottom */
  663.     }
  664.     }
  665.          
  666.   consume_token();
  667.   return rt;
  668. }
  669.  
  670. /* When this function is called, nth_token(0) is the current
  671.    token we are scanning.  This means that the next token we'll
  672.    scan is nth_token (1).  Usually the next token we'll scan
  673.    is nth_token (0) (and the current token is in [yylval,yychar]).  */
  674. tree
  675. arbitrate_lookup (name, exp_decl, type_decl)
  676.      tree name, exp_decl, type_decl;
  677. {
  678.   int ch;
  679.  
  680.   scan_tokens (3);
  681.   ch = nth_token (1)->yychar;
  682.  
  683.   switch (ch)
  684.     {
  685.     case '(':
  686.     case LEFT_RIGHT:
  687.       /* If we guessed wrong here, `build_functional_cast' can fix it.  */
  688.       return type_decl;
  689.  
  690.     case '=':
  691.       if (global_bindings_p ())
  692.     /* Probably a default parameter.  */
  693.     return type_decl;
  694.       /* Probably not an initialization.  */
  695.       return exp_decl;
  696.  
  697.     case '[':
  698.       /* This needs special help because an expression inside the
  699.      brackets means nothing.  */
  700.       {
  701.     int i;
  702.  
  703.     for (i = 0; i < 42; i++)
  704.       {
  705.         int ith_yychar;
  706.  
  707.         scan_tokens (3+i);
  708.         ith_yychar = nth_token (2+i)->yychar;
  709.  
  710.         /* If we hit an undefined identifier, assume
  711.            the decl in arbitration is its type specifier.  */
  712.         if (ith_yychar == IDENTIFIER
  713.         && lookup_name (nth_token (2+i)->yylval.ttype, 0) == 0)
  714.           return type_decl;
  715.         else if (ith_yychar == ']')
  716.           {
  717.         /* There are only a few things we expect after a ']'
  718.            in a declarator.  */
  719.         i += 1;
  720.         scan_tokens (4+i);
  721.         ith_yychar = nth_token (2+i)->yychar;
  722.  
  723.         /* These are inconclusive.  */
  724.         if (ith_yychar == LEFT_RIGHT
  725.             || ith_yychar == '('
  726.             || ith_yychar == '['
  727.             || ith_yychar == ',')
  728.           continue;
  729.         /* stmt or decl?  We'll probably never know.  */
  730.         else if (ith_yychar == ';')
  731.           goto warn_ambiguous;
  732.  
  733.         if (ith_yychar == '=')
  734.           {
  735.             if (nth_token (3+i)->yychar == '{')
  736.               return type_decl;
  737.             continue;
  738.           }
  739.  
  740.         /* Whatever it is, it looks like we're processing an expr.  */
  741.         return exp_decl;
  742.           }
  743.       }
  744.     goto warn_ambiguous;
  745.       }
  746.  
  747.     case ',':
  748.     case ';':
  749.     case '&':
  750.     case '<':
  751.     case '*':
  752.     case ']':
  753.     case ')':
  754.     case '>':
  755.       /* see if the next token looks like it wants to be part
  756.      of a declaration list or an expression list.  */
  757.       {
  758.     int i;
  759.  
  760.     /* Some heuristics: if we are inside a function definition,
  761.        prefer the local declaration.  */
  762.     if (! global_bindings_p ())
  763.       {
  764.         if (IDENTIFIER_LOCAL_VALUE (name) == exp_decl)
  765.           return exp_decl;
  766.         if (IDENTIFIER_LOCAL_VALUE (name) != type_decl
  767.         && IDENTIFIER_CLASS_VALUE (name) == exp_decl)
  768.           return exp_decl;
  769.       }
  770.     /* If these symbols follow in a list, we know it's a list of
  771.        expressions.  */
  772.     if (follows_identifier[nth_token (2)->yychar])
  773.       return exp_decl;
  774.  
  775.     /* If we see a id&, or id&) the we are probably in an argument list. */
  776.     if (ch=='&'
  777.         && (nth_token (2)->yychar == ',' || nth_token (2)->yychar == ')'))
  778.       return type_decl;
  779.  
  780.     /* Look for the first identifier or other distinguishing token
  781.        we find in the next several tokens.  */
  782.     for (i = 0; i < 42; i++)
  783.       {
  784.         int ith_yychar;
  785.  
  786.         scan_tokens (3+i);
  787.         ith_yychar = nth_token (2+i)->yychar;
  788.  
  789.         if (ith_yychar == IDENTIFIER)
  790.           {
  791.         tree as_type = lookup_name (nth_token (2+i)->yylval.ttype, 1);
  792.         if (as_type && TREE_CODE (as_type) != TYPE_DECL)
  793.           return exp_decl;
  794.         /* An undeclared identifier or a typename means we're
  795.            probably looking at a typename.  */
  796.         return type_decl;
  797.           }
  798.         else if (ith_yychar == EMPTY
  799.              || follows_identifier[ith_yychar])
  800.           return exp_decl;
  801.         else if (follows_typename[ith_yychar])
  802.           return type_decl;
  803.         /* stmt or decl?  We'll probably never know.  */
  804.         else if (ith_yychar == ';')
  805.           goto warn_ambiguous;
  806.       }
  807.     goto warn_ambiguous;
  808.       }
  809.  
  810.     default:
  811.       if (follows_identifier[ch])
  812.     return exp_decl;
  813.       if (follows_typename[ch])
  814.     return type_decl;
  815.  
  816.       /* Fall through...  */
  817.     warn_ambiguous:
  818.       warning ("name `%s' could be type or expression; compiler assuming type",
  819.            IDENTIFIER_POINTER (DECL_NAME (type_decl)));
  820.       return type_decl;
  821.     }
  822. }
  823.  
  824. /* now returns decl_node */
  825.  
  826. #if 0
  827. static tree
  828. hack_ptype()
  829. {
  830.   /* when we get here, we know that [0] is a ptype and [1] is '<'.
  831.    * now we loop over simple parameters. */
  832.   struct token this_param;
  833.   int n = 2;
  834.   tree tplist = 0;
  835.   tree tc;
  836.   scan_tokens(n+1);
  837.   
  838.   while((this_param = *nth_token(n)).yychar != '>')
  839.     {
  840.       /* if it is a type, add it to the list */
  841.       tree thistype;
  842.     
  843.       switch(this_param.yychar)
  844.     {
  845.     case IDENTIFIER:
  846.     case TYPENAME:
  847.     case TYPESPEC:
  848.       break;
  849.     default:
  850.       return 0;
  851.     }
  852.  
  853.       thistype = this_param.yylval.ttype;
  854.       thistype = lookup_name(thistype, 1);
  855.       thistype = TREE_TYPE (thistype);
  856.         
  857.       if (tplist)
  858.     tplist = chainon (tplist, build_tree_list (NULL_TREE, thistype));
  859.       else
  860.     tplist = build_tree_list(NULL_TREE, thistype);
  861.     
  862.     
  863.       /* then suck up the comma */
  864.       n++;
  865.       scan_tokens(n+1);
  866.       this_param = *nth_token(n);
  867.       if (this_param.yychar == ',')
  868.     {
  869.       n++;
  870.       scan_tokens(n+1);
  871.       continue;
  872.     }
  873.       if (this_param.yychar == '>')
  874.     break;
  875.       return 0;
  876.     }
  877.  
  878.   /* once we're done, lookup_template_class -> identifier */
  879.   tc = lookup_template_class (nth_token(0)->yylval.ttype,tplist);
  880.   /* then lookup_name on that to get a type, if there is one */
  881.   tc = lookup_name (tc, 1);
  882.   if (tc)
  883.     {
  884.       int i;
  885.       /* don't actually eat the trailing '>'... we can replace it! */
  886.       for (i=0; i<n; i++)
  887.     consume_token();
  888.       /*    IDENTIFIER_TYPE_VALUE (DECL_NAME (tc)) = */
  889.       return DECL_NAME (tc);
  890.     }
  891.   return NULL_TREE;
  892. }
  893. #endif
  894.  
  895. #if 0
  896. static tree
  897. hack_more_ids (n)
  898.      int n;
  899. {
  900.   /*
  901.    * The recursion should probably do consume_tokens(), since once we've started
  902.    * down an IDENTIFIER SCOPE ... chain, we don't need to back-track - we just
  903.    * get as much as we can, make SCOPE_REF's out of it, and return it.
  904.    */
  905.   struct token this_iter, this2_iter;
  906.   int tmp_y;
  907.   
  908.   scan_tokens(n+1);
  909.   this_iter = *nth_token(n);
  910.  
  911.   tmp_y = nth_token(n)->yychar;
  912.   if (tmp_y == IDENTIFIER || tmp_y == TYPENAME)
  913.     {
  914.       scan_tokens(n+2+2);
  915.       if (nth_token(n+1)->yychar == SCOPE)
  916.     {
  917.       if (nth_token(n+1+2)->yychar == SCOPE)
  918.         {
  919.           tree hmi;
  920.     
  921.           consume_token();    /* last IDENTIFIER (this_iter) */
  922.           consume_token();    /* last SCOPE */
  923.           this2_iter = *nth_token(n);
  924.     
  925.           hmi = hack_more_ids (n);
  926.     
  927.           if (hmi)
  928.         return build_parse_node (SCOPE_REF, this_iter.yylval.ttype, hmi);
  929.           consume_token(); /* last IDENTIFIER (this2_iter) */
  930.           return build_parse_node (SCOPE_REF, this_iter.yylval.ttype,
  931.                        this2_iter.yylval.ttype);
  932.         }
  933.       else
  934.         {
  935.           /* consume_token();    */    /* last IDENTIFIER */
  936.           /* leave whatever else we got */
  937.           /* return this_iter.yylval.ttype; */
  938.           return NULL_TREE;
  939.         }
  940.     }
  941.     }
  942.   return NULL_TREE;        /* @@ may need to backtrack */
  943. }
  944. #else
  945. /* niklas@appli.se says:  I didn't understand how the code above was intended
  946.  * to work, so I rewrote it (also changed the interface a bit).  This code
  947.  * dives down an IDENTIFIER/TYPENAME SCOPE ... chain as long as the parsed
  948.  * type prefix constitutes recognizable (by resolve_scope_to_name) types.
  949.  * Interface changed like this:
  950.  * 1. Takes an extra argument containing the name of the the type recognized
  951.  *    so far.
  952.  * 2. Now returns the name of the type instead of a SCOPE_REF. */
  953. static tree
  954. hack_more_ids(n, outer)
  955.   int n;
  956.   tree outer;
  957. {
  958.   int ch;
  959.   tree type, val;
  960.  
  961.   scan_tokens (n + 2);
  962.   if (nth_token (n + 1)->yychar != SCOPE
  963.       || ((ch = nth_token (n + 2)->yychar) != IDENTIFIER && ch != TYPENAME))
  964.     return NULL_TREE;
  965.   val = build_parse_node (SCOPE_REF, outer, nth_token (n + 2)->yylval.ttype);
  966.   type = resolve_scope_to_name (NULL_TREE, val);
  967.   if (type == NULL_TREE)
  968.     return NULL_TREE;
  969.   consume_token ();
  970.   consume_token ();
  971.   val = hack_more_ids (n, type);
  972.   if (! val)
  973.     consume_token ();
  974.   return val ? val : type;
  975. }
  976. #endif
  977.  
  978. #if 0
  979. static struct token
  980. hack_scope ()
  981. {
  982.   /* we've got a :: - what follows is either a global var or a type. */
  983.   /* hmm, template names can be in the global scope too... */
  984.   tree t1;
  985.   struct token rt;
  986.   
  987.   scan_tokens(1);
  988.   if (nth_token(1)->yychar == IDENTIFIER)
  989.     {
  990.       /* @@ this is probably not right, but doesn't get hit yet */
  991.       t1 = build_parse_node (SCOPE_REF,
  992.                  NULL_TREE, /* to get "global" scope */
  993.                  hack_more_ids(0)); /* do some prefetching */
  994.       rt.yylval.ttype = t1;
  995.       rt.yychar =        /*SCOPED_*/TYPENAME;
  996.       return rt;
  997.     }
  998.   else
  999.     {
  1000.       rt = *nth_token(0);
  1001.       consume_token();
  1002.       return rt;
  1003.     }
  1004. }
  1005. #endif
  1006.   
  1007. /*
  1008.  * Generations:
  1009.  *     
  1010.  * PINST: PTYPE { saved_arg_count = arg_count($1) }
  1011.  *        '<' { arg_c = 0; } PARGS '>'
  1012.  *        ;
  1013.  * PARG: TYPE
  1014.  *       | VALUE
  1015.  *       ;
  1016.  * (of course the arg counting doesn't work for recursion... Do it right.)
  1017.  * PARGS: PARG { assert(arg_c == saved_arg_count); }
  1018.  *        | PARG ',' PARGS    { arg_c++; }
  1019.  *        ;
  1020.  * ATYPE: PINST
  1021.  *        | TYPEID
  1022.  *        ;
  1023.  * TYPE: ATYPE
  1024.  *       | ATYPE { basetype = $1; } '::' TYPEKIDS
  1025.  *       ;
  1026.  * TYPEKIDS: TYPE { assert ($1 is a member of basetype); }
  1027.  *       | TYPEKIDS { basetype += $1} TYPE { assert( $3 is in basetype ); }
  1028.  *       ;
  1029.  *
  1030.  *
  1031.  * state0: ; ATYPE
  1032.  *     TYPE '<': ac = args($0), base = CALL state1, state3    
  1033.  *     TYPE '::': base=$0, state3
  1034.  *     else return TYPE
  1035.  * state1: ; begin PARGS
  1036.  *     if(ac < list length) punt
  1037.  *     PARG ",": add to list, state1
  1038.  *     PARG ">": add to list, return
  1039.  *     else unravel
  1040.  * state3: ; begin TYPEKIDS
  1041.  *     TYPE: 
  1042.  */
  1043.   
  1044.   
  1045. #ifdef SPEW_DEBUG    
  1046. /* debug_yychar takes a yychar (token number) value and prints its name. */
  1047. static int
  1048. debug_yychar (yy)
  1049.      int yy;
  1050. {
  1051.   /* In cp-parse.y: */
  1052.   extern char *debug_yytranslate ();
  1053.   
  1054.   int i;
  1055.   
  1056.   if(yy<256) {
  1057.     fprintf (stderr, "<%d: %c >\n", yy, yy);
  1058.     return 0;
  1059.   }
  1060.   fprintf (stderr, "<%d:%s>\n", yy, debug_yytranslate (yy));
  1061.   return 1;
  1062. }
  1063.  
  1064. #endif
  1065.